package com.hp.springmvcControler;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hp.bean.dengl;
import com.hp.bean.houseinfo;
import com.hp.bean.userinfo;
import com.hp.bean.xiang;
import com.hp.mapper.userMapper;

@Controller
public class HelloWord {

	@Autowired
	private userMapper um;

	public void setUm(userMapper um) {
		this.um = um;
	}

	public static final String SUCCESS = "success";// return "success"

	// 添加
	@RequestMapping("**/add")
	public ModelAndView add(houseinfo house, HttpSession session) {
		System.out.println("modelAndView");
		String modelAndViewpage = "forward:/sele";
		ModelAndView modelAndView = new ModelAndView(modelAndViewpage);// 通过构造函数传递转发页面
		String uame = (String) session.getAttribute("uame");
		userinfo userUame = um.userUame(uame);
		int id = userUame.getId();
		System.out.println(id);
		house.setUid(userUame);
		um.inserth(house);
		return modelAndView;
	}

	// 查询所有
	@RequestMapping("/sele")
	public String select(Model model, HttpSession session) {
		List<houseinfo> selectall = um.select();
		model.addAttribute("selectall", selectall);
		for (houseinfo houseinfo : selectall) {
			System.out.println(selectall);
		}
		// System.out.println(selectall.get(0).getUid().getUame());
		String uame = (String) session.getAttribute("uame");
		System.out.println(uame);
		System.out.println("111");
		return "qwe";
	}

	// 查询houseinfo
	@RequestMapping("**/selehouse/{hid}")
	public String selehouse(@PathVariable("hid") int hid, Model model) {
		houseinfo selectId = um.selectId(hid);
		model.addAttribute("selectId", selectId);
		System.out.println("111");
		return "update";
	}

	// 修改
	@RequestMapping("**/upda/{hid}")
	public ModelAndView upda(@PathVariable("hid") int hid, houseinfo house, HttpSession session) {
		System.out.println("修改");
		house.getHid();
		String modelAndViewpage = "forward:/sele";
		ModelAndView modelAndView = new ModelAndView(modelAndViewpage);// 通过构造函数传递转发页面
		String uame = (String) session.getAttribute("uame");
		userinfo userUame = um.userUame(uame);
		int id = userUame.getId();
		System.out.println(id);
		house.setUid(userUame);
		um.updateU(house);
		return modelAndView;
	}

	// 删除
	@RequestMapping("**/delehouse/{hid}")
	public ModelAndView delehouse(@PathVariable("hid") int hid) {
		System.out.println("删除");
		String modelAndViewpage = "forward:/sele";
		ModelAndView modelAndView = new ModelAndView(modelAndViewpage);// 通过构造函数传递转发页面
		um.dalete(hid);
		return modelAndView;
	}

	// 跳转添加
	@RequestMapping("**/addhou")
	public String addhou() {
		return "add";

	}

	// 验证登录用户
	@ResponseBody
	@RequestMapping("/selectname")
	public String denglu1(HttpSession session, dengl den) {
		String name2 = den.getName();
		System.out.println(name2);
		session.setAttribute("name", name2);
		com.hp.bean.dengl selectName = um.selectName(name2);
		if (selectName == null) {
			return "0";
		}
		return "1";
	}

	// 验证登录密码
	@ResponseBody
	@RequestMapping("/selectpass")
	public String selectpass(HttpSession session, dengl den) {

		String password1 = den.getPassword();
		System.out.println(password1);
		String name = (String) session.getAttribute("name");
		dengl selectpass = um.selectpass(name, password1);
		if (selectpass == null) {
			return "0";
		} else {
			return "1";
		}
	}

	// 注册添加
	@RequestMapping("/add")
	public String addU(dengl den) {
		um.insertU(den);
		return "sele";
	}

	// 查询标题
	@RequestMapping("/biaoti")
	public String t() {

		return "biaoti";
	}
	
	// 查询价格
	@RequestMapping("/price")
	public String price(Model model) {
		 List<xiang> selectprice = um.selectbiao();
		 model.addAttribute("selectprice", selectprice);
		return "price";
	}

	// 标题添加
	@RequestMapping("/addbiao")
	public String addG(xiang xia) {
		um.insertG(xia);
		 List<xiang> selectbiao = um.selectbiao();
		return "biaoti";
	}
	
	// 删除所选关键字
		@RequestMapping("/dele")
		public String delexiang() {
			um.daleteX();
			return "price";
		
		}
		// 修改竞价
		@RequestMapping("/updaX")
		public String updaX(xiang xia,Model model) {
			um.updateX(xia);
			List<xiang> selectprice = um.selectbiao();
			 model.addAttribute("selectprice", selectprice);
			return "price2";
			
		}

}
